//
//  ProphesyViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/30.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "ProphesyViewController.h"
#import "ProphesyTableViewCell.h"
#import "RankAttached.h"
#import "RankData.h"
#import "RankModel.h"
#import "RankNewscoverinfo.h"
#import "RankNewsimageinfolist.h"
#import "RankParamap.h"
#import "NetworkTool.h"
#import "ProphesyDetailViewController.h"
#import "UIImageView+WebCache.h"
#define kProphesyTableViewCellReuse @"Prophesy"
@interface ProphesyViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)RankModel *prophesyData;
@property (nonatomic,strong)NSMutableArray *containerArray;
@end

@implementation ProphesyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _containerArray = [NSMutableArray array];
    _prophesyData = [[RankModel alloc] init];
    [self handleData];
//    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 120) style:UITableViewStylePlain];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 112)];
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 190;
    [_tableView registerNib:[UINib nibWithNibName:@"ProphesyTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kProphesyTableViewCellReuse];
}

- (void)handleData{
    
    NSString *urlString = @"http://211.151.51.100/v1/guess/list?source=0&pageSize=10&cursor=-1&type=DOWN";
    
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.3.1 (agent:s;channel:wandoujia;credential:e1MyMDEwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDAyMTMyOTYzLTJlZjRjNThjZDJ9QDE0NzExODgxOTgzODY=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
        
        //        NSLog(@"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%@",result);
        _prophesyData = [RankModel modelWithDic:result];
        
        [self.containerArray addObject:_prophesyData];
        
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{


    return _prophesyData.data.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ProphesyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kProphesyTableViewCellReuse];
//    cell.backView.layer.shadowOffset= CGSizeMake(5, 5);
//    cell.backView.layer.shadowColor = [UIColor grayColor].CGColor;

    cell.title.text = _prophesyData.data[indexPath.section].title;
 
    
    NSURL *url = [NSURL URLWithString:_prophesyData.data[indexPath.section].newsImageInfoList[0].url];
    [cell.mainImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default_image"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ProphesyDetailViewController *prophesyDetail = [[ProphesyDetailViewController alloc] init];
    prophesyDetail.detailLinkString = _prophesyData.data[indexPath.section].shareUrlV2;
    [self presentViewController:prophesyDetail animated:YES completion:^{
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
